//
//  LeftSortsViewController.m
//  UI_Project_A
//
//  Created by GH on 16/3/1.
//  Copyright © 2016年 GH. All rights reserved.
//

#import "LeftSortsViewController.h"
#import "AppDelegate.h"
#import "SetUpController.h"
#import <DKNightVersion.h>
#import <UIImageView+WebCache.h>
#import "Collection.h"
#import "VideoViewController.h"
#import "PushVideoController.h"

@interface LeftSortsViewController ()<UITableViewDelegate,UITableViewDataSource,UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property (nonatomic ,retain)UIView *viewOfBack;
@property (nonatomic ,retain)UIView *viewOfbottom;
@property (nonatomic ,retain)UIImageView *backGroundImage;
@property (nonatomic ,retain)UIButton *buttonOfclick;
@property (nonatomic ,retain)UIView *viewOfimage;
@property (nonatomic ,retain)UIImage *image;



@end

@implementation LeftSortsViewController


- (void) dealloc {

    [_image release];
    [_viewOfimage release];
    [_buttonOfclick release];
    [_backGroundImage release];
    [_switchOfNight release];
    [_viewOfbottom release];
    [_viewOfBack release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];


    self.backGroundImage = [[UIImageView alloc] initWithFrame:self.view.bounds];
    self.backGroundImage.image = [UIImage imageNamed:@"background3.jpg"];
    [self.view addSubview:self.backGroundImage];
    
    
    
    
    
    
    
    self.tableview = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:self.tableview];
    [self.tableview release];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"pool"];

    
    
    //view
    
    self.viewOfBack = [[UIView alloc] initWithFrame:CGRectMake(90, 50, 120, 120)];
    [self.tableview addSubview:self.viewOfBack];
    [self.viewOfBack release];
    self.viewOfBack.layer.masksToBounds = YES;
    self.viewOfBack.layer.cornerRadius = 60;
    
    
    
    
//    self.viewOfimage = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 120, 120)];
//    [self.viewOfBack addSubview:self.viewOfimage];
//    [self.viewOfimage release];
    UIImageView * imagePic = [[UIImageView alloc] init];
    imagePic.frame = CGRectMake(0, 0, 120, 120);
    [self.viewOfBack addSubview:imagePic];
    [imagePic release];
//
    self.image = [UIImage imageNamed:@"52.jpg"];
    [imagePic setImage:self.image];
    
    

    [self createbottomOfView];
    
}






- (void)createbottomOfView {

    self.viewOfbottom = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 160, 300, 60)];
    [self.tableview addSubview:self.viewOfbottom];
    self.switchOfNight = [[UISwitch alloc] initWithFrame:CGRectMake(WIDTH/2 +30, 18, 50, 50)];
    [self.viewOfbottom addSubview:self.switchOfNight];
    [self.switchOfNight addTarget:self action:@selector(handleSwitch:) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel  *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, 100, 50)];
    [self.viewOfbottom addSubview:label];
    label.text = @"夜间模式";
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont systemFontOfSize:21];
    
}


- (void)handleSwitch:(UISwitch *)SWIT {
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    
    if ([DKNightVersionManager currentThemeVersion] == DKThemeVersionNight) {
        
        self.backGroundImage.image = [UIImage imageNamed:@"background3.jpg"];
        [DKNightVersionManager dawnComing];
    } else {
        self.backGroundImage.image = [UIImage imageNamed:@"nightday.jpg"];

        [DKNightVersionManager nightFalling];
    }
    // 保存状态 , 程序再次启动时的偏好设置
    [user setBool:SWIT.on forKey:@"ISNIGHT"];
    [user synchronize];

    NSLog(@"%d",SWIT.on);
    
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];

    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.font = [UIFont systemFontOfSize:20.0f];
    cell.backgroundColor = [UIColor clearColor];
    cell.textLabel.textColor = [UIColor whiteColor];
    
    if (indexPath.row == 0) {
        cell.textLabel.text = @"离线下载";
    } else if (indexPath.row == 1) {
        cell.textLabel.text = @"推送消息";
    } else if (indexPath.row == 2) {
        cell.textLabel.text = @"视听";
    } else if (indexPath.row == 3) {
        cell.textLabel.text = @"收藏";
    } else if (indexPath.row == 4) {
    
    cell.textLabel.text = @"清理缓存";
    }
    
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
//
//    [tempAppDelegate.LeftSlideVC closeLeftView];//关闭左侧抽屉
//    
    if (indexPath.row == 4) {
        //缓存

        [self alerControllerForEdit];

    }else if (indexPath.row == 3) {
        
        Collection * collect = [Collection new];
    

        [self presentViewController:collect animated:YES completion:nil];
        
        
    }else if (indexPath.row == 2) {
    
        VideoViewController *video = [[VideoViewController alloc] init];
        [self presentViewController:video animated:YES completion:nil];
    
        [video release];
    
    }else if (indexPath.row == 1) {
//    
//        PushVideoController *push = [[PushVideoController alloc] init];
//        
//        [self presentViewController:push animated:YES completion:nil];
//
        
        
    }
    
    
}







- (void)alerControllerForEdit {
    
    NSString *str = [NSString stringWithFormat:@"是否清除%.2fM缓存？",[SDImageCache sharedImageCache].getSize / 1000.0 / 1000.0];

    
     UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:str preferredStyle:UIAlertControllerStyleAlert];
    

    /** 添加action */
    UIAlertAction *actionOk = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self clearnHz];
  
    }];

    UIAlertAction *actionDele = [UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    [alert addAction:actionDele];
    [alert addAction:actionOk];

    
    [self presentViewController:alert animated:YES completion:^{
        
    }];


}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 180;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableview.bounds.size.width, 180)];
    view.backgroundColor = [UIColor clearColor];
    return view;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {


    return 80;

}


- (void) clearnHz {

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,  0 ), ^{
    
    NSString  *cachPath = [ NSSearchPathForDirectoriesInDomains ( NSCachesDirectory , NSUserDomainMask ,  YES )  objectAtIndex : 0 ];
    NSArray *files = [[ NSFileManager defaultManager ]  subpathsAtPath :cachPath];
    
    NSLog ( @"files :%ld" ,[files  count]);
    
    
    
    for (NSString *p in files) {
        NSError *error;
        
        NSString *path = [cachPath  stringByAppendingPathComponent :p];
        
        if ([[ NSFileManager defaultManager ]  fileExistsAtPath :path]) {
            
            [[ NSFileManager defaultManager ]  removeItemAtPath :path  error :&error];
            
        }
        
    }
    
    [ self performSelectorOnMainThread : @selector (clearCacheSuccess)  withObject : nil waitUntilDone : YES ];
    
    
    
});
    
    
}

-( void )clearCacheSuccess

{
    
    NSLog ( @" 清理成功 " );
    
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
